<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Post extends Controller_Admin_Base {

    public function before()
    {
        parent::before();
        if ($this->auto_render)
        {
            $this->template->ptitle="分享管理";
        }
    }
    public function __construct($request, $response) {

        $user_model=new Model_Admin_SiteUser();

        $this->users=$user_model->getAdminUser();

        parent::__construct($request, $response);
        
    }
    //分享管理
    public function action_index()
    {
        $model=new Model_Admin_Post();

        $title=$this->request->query('title');

        $email=$this->request->query('email');

        $album=$this->request->query('album');

        $url="/admin/post?t=1";

        if(!empty($title)){

            $url.="&title=".$title;
        }

        if(!empty($email)){

            $url.="&email=".$album;
        }

        if(!empty($album)){

           $url.="&album=".$album;

        }

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $postList=$model->getInfo($email,$album,$title,$offset,$pageSize);

        $pages=ceil((intval($postList['total']))/$pageSize);

        $this->template->title = __('分享管理');

        $view = View::factory('admin/post/index');

        $view->postList =$postList;

        $view->url =$url;

        $view->page= $page;

        $view->pages=$pages;

        $view->title=$title;

        $view->email=$email;
        
        $view->album=$album;

        $view->users=$this->users;

        $this->template->content =  $view;
    }
    //分享专辑管理
    public function action_album()
    {
        $model=new Model_Admin_Post();

        $caption=$this->request->query('caption');

        $email=$this->request->query('email');

        $special=$this->request->query('special');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $albumList=$model->getAlbumInfo($caption,$email,$special,$offset,$pageSize);

        $pages=ceil((intval($albumList['total']))/$pageSize);

        $this->template->title = __('分享专辑');

       
        $view = View::factory('admin/post/album');

        $view->albumList =$albumList;

        $view->page= $page;

        $view->pages=$pages;

        $view->caption=$caption;

        $view->email=$email;

        $view->special=$special;

        $view->users=$this->users;

        $this->template->content =  $view;
    }

    //分享分类管理
    public function action_assort()
    {
        $model=new Model_Admin_Assort();

        $caption=$this->request->query('caption');

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $assortList=$model->getInfo($caption,$offset,$pageSize);

        $pages=ceil((intval($assortList['total']))/$pageSize);

        $this->template->title = __('分享分类');

        $view = View::factory('admin/post/assort');

        $view->assortList =$assortList;

        $view->page= $page;

        $view->pages=$pages;

        $view->caption=$caption;

        $this->template->content =  $view;
    }

    //保存数据
    public function action_saveInfo(){

        $id=$this->request->post('model_id');

        $userid=$this->request->post('userid');

        $album=$this->request->post('album');

        $description=$this->request->post('description');

        $url=$this->request->post('url');

        $tags=$this->request->post('tags');

        $price=$this->request->post('price');

        $isdisabled=$this->request->post('isdisabled');

        $model_post=new Model_Admin_Post();

        echo $model_post->saveInfo($id,$userid,$album,$description,$url,$tags,$price,$isdisabled);
        exit();
    }
    //删除数据
    public function action_deleteInfo(){

        $id=$this->request->post('id');

        $Model_Admin=new Model_Admin_Common();

        echo $Model_Admin->deleteInfo("post",$id);

        exit();

    }
    //根据id加载数据
    public function action_loadInfoById(){

        $id=$this->request->post('id');

        $sql="select t.postid id,t.album,t.title,t.description,t.tags,t.url,t.price,t.isdisabled
            from post t
            where t.postid='$id'
            limit 1";

        $Model_Admin=new Model_Admin_Common();
        
        echo $Model_Admin->loadInfoById($sql);

        exit();
    }
}
